package com.financing.wap.service.impl;


import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.financing.wap.dao.IFpShopperProductDao;
import com.financing.wap.dao.IProductDao;
import com.financing.wap.dto.FpFundInfoDTO;
import com.financing.wap.dto.FpShopperProductDTO;
import com.financing.wap.dto.ProductAnnualRevenueDTO;
import com.financing.wap.dto.ProductDTO;
import com.financing.wap.service.IFpShopperProductService;
import com.financing.wap.util.DTOdateSetUtil;
import com.financing.wap.vo.ShopperProductVO;
/**
 * fp_shopper_product  
 * 用户店铺添加的产品 业务访问层实现类
 * @author huqiyun
 *
 */
 
@Repository("fpShopperProductService")
public class FpShopperProductServiceImpl implements IFpShopperProductService{

	@Autowired
	private IFpShopperProductDao fpShopperProductDao;
	
	@Autowired
	private IProductDao productDao;

	@Override
	public int insert(FpShopperProductDTO fpShopperProduct) throws SQLException {
		DTOdateSetUtil.setCreateInfo(fpShopperProduct);
		DTOdateSetUtil.setUpdateInfo(fpShopperProduct);
		fpShopperProduct.setDeleteTag("1");
		return fpShopperProductDao.insert(fpShopperProduct);
	}

	@Override
	public void update(FpShopperProductDTO fpShopperProduct) throws SQLException {
		DTOdateSetUtil.setUpdateInfo(fpShopperProduct);
		fpShopperProductDao.update(fpShopperProduct);
	}

	@Override
	public void updateForce(FpShopperProductDTO fpShopperProduct) throws SQLException {
		DTOdateSetUtil.setUpdateInfo(fpShopperProduct);
		fpShopperProductDao.updateForce(fpShopperProduct);
	}

	@Override
	public void delete(Integer id) throws SQLException {
		fpShopperProductDao.delete(id);
	}
	
	@Override
	public List<FpShopperProductDTO> queryList(FpShopperProductDTO fpShopperProduct) throws SQLException {
		return fpShopperProductDao.queryList(fpShopperProduct);
	}

	@Override
	public int queryListCount(FpShopperProductDTO fpShopperProduct) throws SQLException {
		return fpShopperProductDao.queryListCount(fpShopperProduct);
	}

	@Override
	public FpShopperProductDTO loadById(Integer id) throws SQLException {
		return fpShopperProductDao.loadById(id);
	}
 	@Override
	public FpShopperProductDTO queryListGetFirst(FpShopperProductDTO fpShopperProduct)
			throws SQLException {
		 List<FpShopperProductDTO> queryList = queryList(fpShopperProduct);
		 if(queryList==null||queryList.size()==0){
			 return new FpShopperProductDTO();
		 }
		 return queryList.get(0);
	}

	@Override
	public List<ShopperProductVO> queryShopperProduct(FpShopperProductDTO shop,Map<String,String> vistCount)
			throws SQLException {
		List<ShopperProductVO> queryShopperProduct = fpShopperProductDao.queryShopperProduct(shop);
		for (ShopperProductVO shopperProductVO : queryShopperProduct) {
			if(!"01".equals(shopperProductVO.getpType()) && "platform".equals(shopperProductVO.getProSource())){
				//非基金 需要查询最高的收益
				ProductAnnualRevenueDTO profitHight = productDao.queryAnnualRevenue(shopperProductVO.getProductDTO().getId(),"h");
				if(profitHight != null) {
					ProductAnnualRevenueDTO profitRe = new ProductAnnualRevenueDTO();
					profitRe.setExpectAnnualRevenue(profitHight.getExpectAnnualRevenue());
					shopperProductVO.getProductDTO().setProfitHight(profitRe);
				}
				if(vistCount!=null){//查询该产品的访问记录
					String c = vistCount.get(shopperProductVO.getProductDTO().getCode());
					c=StringUtils.isBlank(c)?"0":c;
					shopperProductVO.setVistCount(Integer.valueOf(c));
				}
			}else{
				if(vistCount!=null && shopperProductVO.getFpFundInfoDTO()!= null){//查询该产品的访问记录
					String c = vistCount.get(shopperProductVO.getFpFundInfoDTO().getId());
					c=StringUtils.isBlank(c)?"0":c;
					shopperProductVO.setVistCount(Integer.valueOf(c));
				}
			}
		}
		return queryShopperProduct;
	}
	
	@Override
	public int queryShopperProductCount(FpShopperProductDTO shop)
			throws SQLException {
		return fpShopperProductDao.queryShopperProductCount(shop);
	}
	@Override
	public void queryFlagShopForSearch(String userId, List<ProductDTO> productList) throws SQLException {
		for (ProductDTO product : productList) {
			//信托资管pevc产品存储的是code
			String id = String.valueOf(product.getCode());
			String ptype = product.getType();
			FpShopperProductDTO fpShopperProduct=new FpShopperProductDTO();
			fpShopperProduct.setUserId(userId);
			fpShopperProduct.setProductType(ptype);
			fpShopperProduct.setProductId(id);
			List<FpShopperProductDTO> shopList = fpShopperProductDao.queryList(fpShopperProduct);
			for (FpShopperProductDTO fpShopperProductDTO : shopList) {
				String productId = fpShopperProductDTO.getProductId();
				String productType = fpShopperProductDTO.getProductType();
				if(productId.equals(id)&&ptype.equals(productType)){
					product.setFlagShop("true");
					break;
				}
			}
		}
	}
	@Override
	public void queryFlagShop(String userId,String type , List<ProductDTO> productList) throws SQLException {
		FpShopperProductDTO fpShopperProduct=new FpShopperProductDTO();
		fpShopperProduct.setUserId(userId);
		fpShopperProduct.setProductType(type);
		List<FpShopperProductDTO> shopList = fpShopperProductDao.queryList(fpShopperProduct);
		for (ProductDTO product : productList) {
			//信托资管pevc产品存储的是code
			String id = String.valueOf(product.getCode());
			String ptype = product.getType();
			for (FpShopperProductDTO fpShopperProductDTO : shopList) {
				String productId = fpShopperProductDTO.getProductId();
				String productType = fpShopperProductDTO.getProductType();
				if(productId.equals(id)&&ptype.equals(productType)){
					product.setFlagShop("true");
					break;
				}
			}
		}
	}

	@Override
	public void queryFlagShopFund(String userId, List<FpFundInfoDTO> list) throws SQLException {
		FpShopperProductDTO fpShopperProduct=new FpShopperProductDTO();
		fpShopperProduct.setUserId(userId);
		fpShopperProduct.setProductType("01");
		List<FpShopperProductDTO> shopList = fpShopperProductDao.queryList(fpShopperProduct);
		for (FpFundInfoDTO product : list) {
			//基金    判断id
			String id = String.valueOf(product.getId());
			for (FpShopperProductDTO fpShopperProductDTO : shopList) {
				String productId = fpShopperProductDTO.getProductId();
				if(productId.equals(id)){
					product.setFlagShop("true");
					break;
				}
			}
		}
	}

}
